﻿Imports System.IO
Imports System.Text
Imports MpqReader
Public Class frmOptions
    
    Private Sub frmOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.MPQList.Item(0) = "" Then
            Dim RegKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Blizzard Entertainment\World of Warcraft\")
            Dim DataLoc As String = RegKey.GetValue("InstallPath") + "Data\"
            txtCommon.Text = DataLoc + "common.mpq"
            txtPatch.Text = DataLoc + "patch.mpq"
            txtCommon2.Text = DataLoc + "common-2.mpq"
            txtPatch2.Text = DataLoc + "patch-2.mpq"
            txtExpansion.Text = DataLoc + "expansion.mpq"
            txtLichKing.Text = DataLoc + "lichking.mpq"
        Else
            txtCommon.Text = My.Settings.MPQList(0)
            txtCommon2.Text = My.Settings.MPQList(1)
            txtPatch.Text = My.Settings.MPQList(2)
            txtPatch2.Text = My.Settings.MPQList(3)
            txtExpansion.Text = My.Settings.MPQList(4)
            txtLichKing.Text = My.Settings.MPQList(5)
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        MPQLoading()
    End Sub
    Private Sub MPQLoading()
        frmMain.tvModels.Nodes.Clear() 'Clears all nodes for the revamp.
        frmMain.tvModels.Nodes.Add("Characters")
        frmMain.tvModels.Nodes.Add("Creatures")
        frmMain.tvModels.Nodes.Add("Items")
        'Saves the locations so that they're there on next load.
        My.Settings.MPQList.Insert(0, txtCommon.Text)
        My.Settings.MPQList.Insert(1, txtCommon2.Text)
        My.Settings.MPQList.Insert(2, txtPatch.Text)
        My.Settings.MPQList.Insert(3, txtPatch2.Text)
        My.Settings.MPQList.Insert(4, txtExpansion.Text)
        My.Settings.MPQList.Insert(5, txtLichKing.Text)

        Dim myMPQ As New MPQ

        Dim Ary = 0 'Makes the MPQ reader go through all five MPQ's. Increase this as we add more.

        myMPQ.PopulateFileList(My.Settings.MPQList(0))

        For Each a As MPQ.FileListEntry In myMPQ.FileList.Values
            If a.Path.Contains(".m2") Then
                If a.Path.ToLower.StartsWith("character\") Then
                    frmMain.tvModels.Nodes(3).Nodes(0).Nodes.Add(a.Path)
                ElseIf a.Path.ToLower.StartsWith("creature\") Then
                    frmMain.tvModels.Nodes(3).Nodes(1).Nodes.Add(a.Path)
                ElseIf a.Path.ToLower.StartsWith("item\") Then
                    frmMain.tvModels.Nodes(3).Nodes(2).Nodes.Add(a.Path)
                End If
            End If
        Next
        Me.Close()
    End Sub

    Private Sub btnCommon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommon.Click
        OpenMPQFile.FileName = txtCommon.Text
        OpenMPQFile.Tag = txtCommon
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub btnPatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatch.Click
        OpenMPQFile.FileName = txtPatch.Text
        OpenMPQFile.Tag = txtPatch
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub btnCommon2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommon2.Click
        OpenMPQFile.FileName = txtCommon2.Text
        OpenMPQFile.Tag = txtCommon2
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub btnPatch2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatch2.Click
        OpenMPQFile.FileName = txtPatch2.Text
        OpenMPQFile.Tag = txtPatch2
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub btnExpansion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExpansion.Click
        OpenMPQFile.FileName = txtExpansion.Text
        OpenMPQFile.Tag = txtExpansion
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub btnLichKing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLichKing.Click
        OpenMPQFile.FileName = txtLichKing.Text
        OpenMPQFile.Tag = txtLichKing
        'OpenMPQFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenMPQFile.Filter = "MPQ File (*.mpq)|*.mpq"
        OpenMPQFile.ShowDialog()
    End Sub

    Private Sub OpenMPQFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenMPQFile.FileOk
        CType(OpenMPQFile.Tag, Windows.Forms.TextBox).Text = OpenMPQFile.FileName
    End Sub

End Class